package com.css.gdzj.gdzjbxgd.modules.reportinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.gdzj.gdzjbxgd.common.util.ServiceUtil;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestPtjobChildNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestPtjobChildRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestPtjobChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.HonestPtjobChildService;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.viewobjects.HonestPtjobChildQueryParams;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Service
public class HonestPtjobChildServiceImpl implements HonestPtjobChildService {

    @Autowired
    private HonestPtjobChildRepository honestPtjobChildRepository;
    @Autowired
    private HonestPtjobChildNativeRepository honestPtjobChildNativeRepository;
    @Autowired
    HttpServletRequest request;
    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ServiceUtil serviceUtil;

    @Override
    public List<HonestPtjobChildEntity> getList(String bxgdReportInfoId) {
        List<HonestPtjobChildEntity> honestPtjobChildEntities = this.honestPtjobChildNativeRepository.getList(bxgdReportInfoId);
        return (null != honestPtjobChildEntities && honestPtjobChildEntities.size() > 0) ? honestPtjobChildEntities : null;
    }
    /*@Override
    public JSONObject queryHonestPtjobChilds(HonestPtjobChildQueryParams queryParams, int curPage, int pageSize) {
        Page<HonestPtjobChildEntity> honestPtjobChildPage = this.honestPtjobChildNativeRepository.queryHonestPtjobChilds(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(honestPtjobChildPage);
        return jsonObject;
    }


    @Override
    public HonestPtjobChildEntity getHonestPtjobChild(String id) {
        HonestPtjobChildEntity honestPtjobChild = this.honestPtjobChildRepository.findById(id).orElse(null);
         return honestPtjobChild;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HonestPtjobChildEntity saveHonestPtjobChild(HonestPtjobChildEntity honestPtjobChild) {
        String uuid = UUIDGenerator.getUUID();
        honestPtjobChild.setUuid(uuid);
        honestPtjobChild.setCreateUser(PlatformSessionUtils.getUserId());
        honestPtjobChild.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.honestPtjobChildRepository.save(honestPtjobChild);
        return honestPtjobChild;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HonestPtjobChildEntity updateHonestPtjobChild(HonestPtjobChildEntity honestPtjobChild) {
        honestPtjobChild.setUpdateUser(PlatformSessionUtils.getUserId());
        honestPtjobChild.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.honestPtjobChildRepository.save(honestPtjobChild);
        return honestPtjobChild;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHonestPtjobChilds(List<String> ids) {
        List<HonestPtjobChildEntity> honestPtjobChildList = this.honestPtjobChildRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(honestPtjobChildList) && honestPtjobChildList.size() > 0) {
            for(HonestPtjobChildEntity honestPtjobChild : honestPtjobChildList) {
                this.honestPtjobChildRepository.delete(honestPtjobChild);
            }
        }
    }
    @Override
    public void exportExcel(HonestPtjobChildQueryParams queryParams, HttpServletResponse response){
    List<HonestPtjobChildEntity> honestPtjobChildList = honestPtjobChildNativeRepository.queryHonestPtjobChild(queryParams);
    List<HonestPtjobChildExportExcelVO> excelVos = new ArrayList<>();

    honestPtjobChildList.forEach(e->{
    HonestPtjobChildExportExcelVO honestPtjobChildExcelVo = new HonestPtjobChildExportExcelVO();
    BeanUtils.copyProperties(e,honestPtjobChildExcelVo);
    excelVos.add(honestPtjobChildExcelVo);
    });
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "honest_ptjob_child"), HonestPtjobChildExportExcelVO.class, excelVos);
    PlatformDownloadUtils.exportExcel(workbook, "honest_ptjob_child.xls", response);

    }
    @Override
    public String uploadFile(MultipartFile file, HttpServletResponse response) {
    try{
    StringBuilder returnMsg = new StringBuilder();
    ImportParams params = new ImportParams();
    params.setHeadRows(1);
    params.setNeedVerify(true);

    ExcelImportResult<HonestPtjobChildExcelVO> result = ExcelImportUtil.importExcelMore(file.getInputStream(),HonestPtjobChildExcelVO.class,params);
    List<HonestPtjobChildExcelVO> honestPtjobChildExcelVoList = result.getFailList();
    for(int i = 0;i<honestPtjobChildExcelVoList.size();i++){
        HonestPtjobChildExcelVO honestPtjobChildExcelVo = honestPtjobChildExcelVoList.get(i);
        String msg = honestPtjobChildExcelVo.getErrorMsg();
        Integer num = honestPtjobChildExcelVo.getRowNum();
        if(PlatformObjectUtils.checkFieldAllNull(honestPtjobChildExcelVo)){
        honestPtjobChildExcelVoList.remove(i);
        i--;
    }else{
        honestPtjobChildExcelVo.setRowNum(num);
        honestPtjobChildExcelVo.setErrorMsg(msg);
            }
    }

    List<HonestPtjobChildEntity>  dataEntityList = new ArrayList<>();
    List<HonestPtjobChildExcelVO> list = result.getList();
            list.forEach(e->{
            HonestPtjobChildEntity dataEntity = new HonestPtjobChildEntity();
            BeanUtils.copyProperties(e,dataEntity);
            dataEntity.setId(UUIDGenerator.getUUID());
            dataEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            dataEntity.setCreateUser(PlatformSessionUtils.getUserId());
            dataEntityList.add(dataEntity);
            });
    honestPtjobChildRepository.saveAll(dataEntityList);
    int successListSize = list.size() - honestPtjobChildExcelVoList.size();
    returnMsg.append("成功导入" + successListSize + "条，失败" + honestPtjobChildExcelVoList + "条");

    for (HonestPtjobChildExcelVO entity : result.getFailList()) {
        String msg = "";
        returnMsg.append("第" + entity.getRowNum() + "行" + entity.getErrorMsg() + "<br/>");
        returnMsg.append(msg);
    }
    return returnMsg.toString();
    }catch (Exception e){
        e.printStackTrace();
    return "导入失败，请检查数据正确性";

    }
   }*/

}
